class Node {
    constructor(value,next = null) {
        this.value = value
        this.next = next
    }
}

function listNode(){
    const head = new Node(1)
    head.next = new Node(2)
    head.next.next = new Node(3)
    head.next.next.next = new Node(4)
    let p = head,ret = '';
    while(p){
        ret += ` ${p.value} =>`;
        p = p.next;
    }
    ret += 'null';
    console.log('ret: ',ret); //ret:   1 => 2 => 3 => 4 =>null
    
}
// listNode()

/* 
* 双数组实现链表
*/


/* 
需要生成：a->d->e->c->f->b

data    a   b   c   d   e   f 
next    3  null 5   4   2   1  代表下一个节点的下标     
下标     0   1   2   3   4   5

*/
function listNode2(){
    const data = [] //存放数据
    const next = [] //存放指针

    /**
     * 
     * @param {*} ind 在ind节点后面插入新节点
     * @param {*} p 节点在data数组中下标
     * @param {*} val 节点的值
     */
    function addNode(ind,p,val){
        data[p] = val
        next[p] = next[ind]
        next[ind] = p
    }
    let head = 3
    data[3] = 'a'
    addNode(3,5,'b')
    addNode(5,7,'c')
    addNode(7,2,'d')
    addNode(2,1,'e')
    addNode(7,4,'f')
    let p = head,ret = '';
    while(p){
        ret += ` ${data[p]} =>`;
        p = next[p];
    }
    ret += 'null';
    console.log('ret: ',ret);  //ret:   a => b => c => f => d => e =>null
}
listNode2()
